# coding=utf-8
import os
import sys


def print_file(file):
    f = open(file, "r+", encoding="utf-8")
    line = f.readline()
    while line:
        print(line, end="")
        line = f.readline()
    f.close()


def print_file1(file):
    f = open(file, "r+", encoding="utf-8")
    lines = f.readlines()
    for line in lines:
        print(line, end="")
    f.close()


def rep_file(file, oldStr, newStr):
    f = open(file, "r", encoding="utf-8")
    lines = f.readlines()
    for i in range(len(lines) - 1):
        lines[i] = lines[i].replace(oldStr, newStr)
    f.flush()
    f.close()

    fd = open(file, "w", encoding="utf-8")
    fd.writelines(lines)
    fd.flush()
    fd.close()


def get_file_path(file_name):
    project_name = "pytest"
    file_dir = "files"
    path = os.path.abspath(".")
    ps = path.split(project_name)
    return ps[0] + project_name + "\\" + file_dir + "\\" + file_name


# file = "C:/Users/xiaopan/Desktop/temp4.txt"
file_name = "file.txt"
# prin_file(file)
# prin_file1(file)


print_file1(get_file_path(file_name))
rep_file(get_file_path(file_name), "data", "ds")
print_file(get_file_path(file_name))


# print(sys.getdefaultencoding())
